package gestionmagasins_avec_patterns.forms;

import gestionmagasins_avec_patterns.model.CatalogueUsagers;
import gestionmagasins_avec_patterns.model.Usager;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class LoginJFrame extends javax.swing.JFrame 
{
    public LoginJFrame() 
    {
        initComponents();
        
        this.setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        usernameJText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        loginJButton = new javax.swing.JButton();
        passwordJText = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Super Logiciel inc. AVEC Patterns");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setText("Connexion");

        usernameJText.setName("nom d'utilisateur"); // NOI18N
        usernameJText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                LoginJFrame.this.keyPressed(evt);
            }
        });

        jLabel2.setText("Nom d'utilisateur");

        jLabel3.setText("Mot de passe");

        loginJButton.setText("Entrer");
        loginJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loginJButtonMouseClicked(evt);
            }
        });
        loginJButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                LoginJFrame.this.keyPressed(evt);
            }
        });

        passwordJText.setName("mot de passe"); // NOI18N
        passwordJText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                LoginJFrame.this.keyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usernameJText)
                            .addComponent(passwordJText, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(loginJButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameJText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(passwordJText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(loginJButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginJButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginJButtonMouseClicked
        login();

    }//GEN-LAST:event_loginJButtonMouseClicked

    private void keyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            login();
    }//GEN-LAST:event_keyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton loginJButton;
    private javax.swing.JPasswordField passwordJText;
    private javax.swing.JTextField usernameJText;
    // End of variables declaration//GEN-END:variables

    private void showError(String errorMessage) 
    {   
        JOptionPane.showMessageDialog(this, errorMessage, "Erreur!", JOptionPane.ERROR_MESSAGE);
    }

    private boolean validateFieldsNotEmpty() 
    {
        String errorMessage = "";
        
        JTextField[] jTexts = {usernameJText, passwordJText};
        
        for(JTextField jText : jTexts)
        {
            if(jText.getText().isEmpty())
                errorMessage += "Le champ " + jText.getName() + " ne peut être vide!\n";
        }
        
        if(!errorMessage.equals(""))
        {
            showError(errorMessage);
            
            return false;
        }
        else
            return true;
    }

    private void login() 
    {
        if(validateFieldsNotEmpty())
        {
            String username = usernameJText.getText();
            String password = new String(passwordJText.getPassword());

            Usager usager = CatalogueUsagers.getInstance().findActivated(username, password);

            if(usager != null)
            {
                usernameJText.setText("");
                passwordJText.setText("");
                usernameJText.requestFocus();
                
                this.setVisible(false);
                
                new MainMenuJFrame(this, usager).setVisible(true);
            }
            else
                this.showError("Le nom d'usager/mot de passe est invalide!");
        }
    }
}
